//
//  SHComposePictureViewCell.swift
//  SHWeibo
//
//  Created by jingming wang on 16/8/4.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit

class SHComposePictureViewCell: UICollectionViewCell {
    // 定义一个闭包
    var closure:(()->())?
    // 定义一个属性供外界赋值
    var image: UIImage? {
        didSet{
            // 添加加号图片
            if image == nil {
                bgImageView.image = UIImage(named: "compose_pic_add")
                // 设置高亮图片
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                // 隐藏删除按钮
                deleteButton.hidden = true
            }else {
                bgImageView.image = image
                bgImageView.highlightedImage = nil
                // 显示删除按钮
                deleteButton.hidden = false
            }
        }
    }
    
    // MARK: - 懒加载控件
    // 配图
    private lazy var bgImageView: UIImageView = UIImageView()
    // 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        // 添加点击事件
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置界面
    private func setupUI(){
        // 添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteButton)
        // 添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.top.trailing.equalTo(contentView)
        }
        
    }
    
    // MARK: - 监听方法
    @objc private func buttonClick(){
        closure?()
    }
    
}
